//
//  VideoFeedCellBtn.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/18.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit

//封装公共的侧边栏按钮
class VideoFeedCellBtn: UIControl {
    public let imageView:UIImageView
    public let label:UILabel
    
    required init() {
        imageView = UIImageView()
        label  = UILabel(text:"0",font:.systemFont(ofSize:12))
        super.init(frame:CGRect.zero)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        label.textColor = .white
        self.addSubview(label)
        self.addSubview(imageView)
        label.translatesAutoresizingMaskIntoConstraints = false  //不需要布局约束
        imageView.translatesAutoresizingMaskIntoConstraints = false
        
        imageView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        imageView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        imageView.topAnchor.constraint(equalTo: topAnchor).isActive  = true
        
        label.topAnchor.constraint(equalTo: imageView.bottomAnchor).isActive = true
        label.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    }
    
    //一根或者多根手指开始触摸view（手指按下）
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        imageViewScaleAnimation(isPressed: true)
    }
    
    //某个系统事件(例如电话呼入)打断触摸过程
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        imageViewScaleAnimation(isPressed: false)
    }
    
    //一根或者多根手指离开view（手指抬起）
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        imageViewScaleAnimation(isPressed: false)
    }
    
    //这里使用缩放动画
    private func imageViewScaleAnimation(isPressed:Bool){
        let scale = isPressed ? CGAffineTransform(scaleX: 1.2, y: 1.2) : CGAffineTransform(scaleX: 1, y: 1)
        
        UIView.animate(withDuration: 0.1) {
            self.imageView.transform = scale
        }
    }
}
